open(FH,"</home/uec-00/yapingli/software/gatk_read/Sting/resources/Normal_SNP.filterCNV.txt") or die;
my @in=<FH>;
chomp(@in);
close(FH);
my @out;
my @out_bed;
my %cytosine_hash=();
foreach my $line(@in){
	my @splitin=split "\t",$line;
	if(($splitin[5] eq $splitin[6] && ($splitin[5] eq "T" || $splitin[5] eq "A")) || ($splitin[5] ne $splitin[6] && (($splitin[5] eq "T" && $splitin[6] eq "A") || ($splitin[5] eq "A" && $splitin[6] eq "T"))))
		next;
	my $key = "chr".$splitin[1]."~".$splitin[2];
	my $value = 0;
	push(@out,"$line\n");
	my $start = $splitin[2] - 1;
	my $end = $splitin[2];
	my $chr = "chr".$splitin[1];
	my $name = $splitin[0];
	push(@out_bed,"$chr\t$start\t$end\t$name\t0\t+\n");
}
open(OUT,">/home/uec-00/yapingli/software/gatk_read/Sting/resources/Normal_SNP.filterCNV.cytosine.txt");
print OUT @out;
close(OUT);
open(OUT,">/home/uec-00/yapingli/software/gatk_read/Sting/resources/Normal_SNP.filterCNV.cytosine.bed");
print OUT @out_bed;
close(OUT);
print "index finished\n";
open(FH,"</home/uec-00/yapingli/data/fastaqOTBcolon/CpG_context_normalMerge_All.NODUPS.fastq_bismark.txt") or die;
while(my $line = <FH>){
	chomp($line);
	next if $line =~ /^Bismark/;
	my @splitin=split "\t",$line;
	my $search_key = $splitin[2]."~".$splitin[3];
	if( exists $cytosine_hash{$search_key} ){
		$cytosine_hash{$search_key}++;
	}
}
close(FH);
print "CpG\n";
open(FH,"</home/uec-00/yapingli/data/fastaqOTBcolon/CHG_context_normalMerge_All.NODUPS.fastq_bismark.txt") or die;
while(my $line = <FH>){
	chomp($line);
	next if $line =~ /^Bismark/;
	my @splitin=split "\t",$line;
	my $search_key = $splitin[2]."~".$splitin[3];
	if( exists $cytosine_hash{$search_key} ){
		$cytosine_hash{$search_key}++;
	}
}
close(FH);
print "CHG\n";
open(FH,"</home/uec-00/yapingli/data/fastaqOTBcolon/CHH_context_normalMerge_All.NODUPS.fastq_bismark.txt") or die;
while(my $line = <FH>){
	chomp($line);
	next if $line =~ /^Bismark/;
	my @splitin=split "\t",$line;
	my $search_key = $splitin[2]."~".$splitin[3];
	if( exists $cytosine_hash{$search_key} ){
		$cytosine_hash{$search_key}++;
	}
}
close(FH);
print "CHH\n";

open(OUT,">/home/uec-00/yapingli/data/fastaqOTBcolon/bismark.in1Marray.cytosine.bed");
foreach $k (sort keys %cytosine_hash){ 
	my @splitin = split "~",$k;
	my $start = $splitin[1] - 1;
	my $end = $splitin[1];
	my $chr = $splitin[0];
	my $value = $cytosine_hash{$k};
	my $line = "$chr\t$start\t$end\t$value\t0\t+\n";
	print OUT $line; 
}

close(OUT);
